<?
$url = 'http://scheduleman.org/schedules/'.$_GET['cal'].'.ics';
if ($fp = fopen($url, 'r'))
{
    $content = '';
    // keep reading until there's nothing left
    while ($line = fread($fp, 1024)) {
        $content .= $line;
    }
    $pattern = "/DURATION:PT(.*?)S/";
    preg_match_all($pattern, $content, $matches);
    
    $patterns = array();
    $replacements = array();
    
    foreach($matches[1] as $match)
    {
        $seconds = (int) $match;
        $minutes = $seconds / 60;
        if ($minutes <= 60)
        {
            $patterns[] = "/DURATION:PT".$match."S/";
            $replacements[] = "DURATION:PT".(((int)$match)/60)."M";
        }
        else
        {
            $patterns[] = "/DURATION:PT".$match."S/";
            $remainder = $minutes % 60;
            $hours = (int)($minutes/60);
            $replacements[] = "DURATION:PT".$hours."H".$remainder."M";
            
        }
    }
    
    print preg_replace($patterns, $replacements, $content);
    
} else {
   // an error occured when trying to open the specified url
}
?>